package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.CaseAjxxRelation;
import cn.com.css.taxInspection.attention.entity.JaajxxYjqlsjEntity;
import cn.com.css.taxInspection.attention.mapper.JaajxxYjqlsjMapper;
import cn.com.css.taxInspection.attention.service.JaajxxYjqlsjService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class JaajxxYjqlsjServiceImpl extends ServiceImpl<JaajxxYjqlsjMapper, JaajxxYjqlsjEntity> implements JaajxxYjqlsjService {
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private JaajxxYjqlsjMapper jaajxxYjqlsjMapper;
    @Override
    public int importJaajxxYjqlsjDatas(List<Map<String, Object>> ajxxList, List<Map<String, String>> allmaps, List<String> existsAjbhs ) throws ParseException {
        //先删除 在新增 覆盖
        // 创建查询条件
        QueryWrapper<JaajxxYjqlsjEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("AJBH", existsAjbhs); // 假设这里是要根据的字段名
        // 查询符合条件的记录的主键值
        List<JaajxxYjqlsjEntity> list = jaajxxYjqlsjMapper.selectList(queryWrapper);
        // 取出主键值
        List<String> ids = list.stream().map(JaajxxYjqlsjEntity::getId).collect(Collectors.toList());
        // 批量删除
        jaajxxYjqlsjMapper.deleteBatchIds(ids);
        //

        List<JaajxxYjqlsjEntity> inserts = new ArrayList<>();
        JaajxxYjqlsjEntity cajxx;
        for (Map map : ajxxList) {
            cajxx = new JaajxxYjqlsjEntity();
            cajxx.setCaseId("");
            String ajbh  =String.valueOf(map.get("JCAJBH"));
            cajxx.setAjbh(ajbh);
            cajxx.setNsrmc(String.valueOf(map.get("NSRMC")));
            cajxx.setNsrsbh(String.valueOf(map.get("NSRSBH")));
            cajxx.setJcks(String.valueOf(map.get("JCBM_DM")));
            cajxx.setJcr(String.valueOf(map.get("JCRY")));
            cajxx.setJcajxxuuid(String.valueOf(map.get("JCAJXXUUID")));
            cajxx.setSlry(String.valueOf(map.get("SLRY")));
            cajxx.setAjjcztDm(String.valueOf(map.get("AJJCZT_DM")));
            cajxx.setAjmc(String.valueOf(map.get("AJMC")));
            cajxx.setLarq(DateUtils.formatDate(String.valueOf(map.get("LARQ")),"yyyy-MM-dd"));
            cajxx.setZxry(String.valueOf(map.get("ZXRY")));
            cajxx.setAjztDm(String.valueOf(map.get("AJJCZTDM")));
            cajxx.setJcksmc(String.valueOf(map.get("JCKSMC")));
            cajxx.setLrrDm(httpSessionService.getCurrentUserId());
            cajxx.setCreateTime(new Date());
            String yjqlTime ="";
            for (Map<String, String> data : allmaps) {
                if (data.containsKey(ajbh)) {
                    yjqlTime = data.get(ajbh);
                     break;
                }
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dateyjqlTime = dateFormat.parse(yjqlTime);
            cajxx.setYjqlTime(dateyjqlTime);
            inserts.add(cajxx);
        }
        this.saveBatch(inserts);
        return ajxxList.size();
    }
}